package org.example.filepath;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;

public class GetFilePath1 {
    public static void main(String[] args) {
        Path path1 = Paths.get("/home/mkyong/test/file.txt");
        System.err.println("\n[Path]:" + path1);
        printPath(path1);
        System.err.println(String.join("", Collections.nCopies(50, "-")));

        Path path2 = Paths.get("file.txt");
        System.err.println("\n[Path]:" + path2);
        printPath(path2);
        System.err.println(String.join("",Collections.nCopies(50,"-")));

        Path path3 = Paths.get("/home/johnconnor/build.prop");
        System.err.println("\n[Path]:" + path3);
        printPath(path3);
        System.err.println(String.join("", Collections.nCopies(50, "-")));

        Path path4 = Paths.get("/home/mkyong/test/../file.txt");
        System.err.println("\n[Path]:" + path4);
        printPath(path4);
    }

    static void printPath(Path path){

        System.err.printf("%-25s : %s%n","path",path);
        System.err.printf("%-25s : %s%n","Path.toAbsolutePath()",path.toAbsolutePath());
        System.err.printf("%-25s : %s%n","path.getParent()",path.getParent());
        System.err.printf("%-25s : %s%n","path.getRoot()",path.getRoot());
        try {
            if (Files.notExists(path)) {
                return;
            }
            System.err.printf("%-25s : %s%n","path.toRealPath()",path.toRealPath());
            System.err.printf("%-25s : %s%n","path.toRealPath(nofollow)",path.toRealPath(LinkOption.NOFOLLOW_LINKS));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
